package com.canplay.screen.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5签名
 */
class MD5SignatureGenerator {
    private static final String SALT = "test001";

    public static String generateSaltedMD5Signature(String timestamp, String
            nonceStr) {
        // 构造待签名字符串
        String signString = timestamp + "\n" + nonceStr + "\n" + SALT;
        try {
            // 创建 MD5 摘要对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 更新摘要信息
            md.update(signString.getBytes());
            // 计算摘要
            byte[] digest = md.digest();
            // 将摘要转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        // 示例请求参数
        String timestamp = "11111"; // 请求时间戳
        String nonceStr = "11111"; // 请求随机串
        // 生成带盐的签名
        String saltedSignature = generateSaltedMD5Signature(timestamp, nonceStr);
        System.out.println("Salted Signature: " + saltedSignature);
    }
}
